package com.ruoyi.health.report.controller;


import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.health.report.domain.HealthReportStatistics;
import com.ruoyi.health.report.service.IHealthReportStatisticsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 健康报告主表Controller
 *
 * @author ruoyi
 * @date 2023-10-25
 */
@RestController
@RequestMapping("/healthReportStatistics")
public class HealthReportStatisticsController extends BaseController
{
    @Autowired
    private IHealthReportStatisticsService healthReportStatisticsService;

    /**
     * 获取客户指定类型的健康报告数据
     * @param customerId 客户ID
     * @param reportType 报告类型 (daily/weekly/monthly)
     * @param reportPeriod 报告周期 (如: 2023-10-25 / 2023-W43 / 2023-10)
     * @return 健康报告数据
     */
    @GetMapping(value = "/{customerId}/{reportType}")
    public AjaxResult getHealthReportByType(
            @PathVariable("customerId") Long customerId,
            @PathVariable("reportType") String reportType,
            @RequestParam(value = "reportPeriod", required = false) String reportPeriod) {
        clearPage();
        HealthReportStatistics report;

        if (reportPeriod != null && !reportPeriod.isEmpty()) {
            // 查询指定周期的报告
            report = healthReportStatisticsService.selectHealthReportByCustomerTypeAndPeriod(customerId, reportType, reportPeriod);
        } else {
            // 查询最新的报告
            report = healthReportStatisticsService.selectLatestHealthReportByCustomerAndType(customerId, reportType);
        }

        return AjaxResult.success(report);
    }

    /**
     * 获取客户可用的报告周期列表
     * @param customerId 客户ID
     * @param reportType 报告类型 (daily/weekly/monthly)
     * @return 报告周期列表
     */
    @GetMapping(value = "/periods/{customerId}/{reportType}")
    public AjaxResult getAvailablePeriods(
            @PathVariable("customerId") Long customerId,
            @PathVariable("reportType") String reportType) {

        List<String> periods = healthReportStatisticsService.selectAvailablePeriodsByCustomerAndType(customerId, reportType);
        return AjaxResult.success(periods);
    }

    /**
     * 获取完整健康报告数据（包含指标详细信息）
     * @param customerId 客户ID
     * @param reportType 报告类型
     * @param reportPeriod 报告周期
     * @return 完整健康报告数据
     */
    @GetMapping(value = "/full/{customerId}/{reportType}")
    public AjaxResult getFullHealthReport(
            @PathVariable("customerId") Long customerId,
            @PathVariable("reportType") String reportType,
            @RequestParam(value = "reportPeriod", required = false) String reportPeriod) {

        Map<String, Object> result = healthReportStatisticsService.getFullHealthReportData(customerId, reportType, reportPeriod);
        return AjaxResult.success(result);
    }
    @GetMapping(value = "/getTimeBaseData/{customerId}/{timeType}")
    public AjaxResult getTimeBaseData(
            @PathVariable("customerId") Long customerId,
            @PathVariable("timeType") String timeType,
            @RequestParam Map<String, String> params) {
        return AjaxResult.success(healthReportStatisticsService.getTimeBaseData(customerId, timeType, params));
    }


    @GetMapping(value = "/createHealthReport")
    public void createHealthReport(@Param("date")String date){
        Date nowDate = DateUtils.getNowDate();
        if(date != null){
            nowDate = DateUtils.parseDate(date);
        }
        healthReportStatisticsService.createHealthReportByDay(nowDate);
    }

    @GetMapping(value = "/createHealthReportByWeek")
    public void createHealthReportByWeek(@Param("date")String date){
        Date nowDate = DateUtils.getNowDate();
        if(date != null){
            nowDate = DateUtils.parseDate(date);
        }
        healthReportStatisticsService.createHealthReportByWeek(nowDate);
    }
    @GetMapping(value = "/createHealthReportByMonth")
    public void createHealthReportByMonth(@Param("date")String date){
        Date nowDate = DateUtils.getNowDate();
        if(date != null){
            nowDate = DateUtils.parseDate(date);
        }
        healthReportStatisticsService.createHealthReportByMonth(nowDate);
    }

    @InnerAuth
    @PostMapping(value = "/task/createHealthReportByWeekTasK")
    public void createHealthReportByWeekTasK(@Param("date")String date){
        Date nowDate = DateUtils.getNowDate();
        if(date != null){
            nowDate = DateUtils.parseDate(date);
        }
        healthReportStatisticsService.createHealthReportByWeek(nowDate);
    }

    @InnerAuth
    @PostMapping(value = "/task/createHealthReportByMonthTasK")
    public void createHealthReportByMonthTasK(@Param("date")String date){
        Date nowDate = DateUtils.getNowDate();
        if(date != null){
            nowDate = DateUtils.parseDate(date);
        }
        healthReportStatisticsService.createHealthReportByMonth(nowDate);
    }

    @InnerAuth
    @PostMapping(value = "/task/createHealthReportTask")
    public void createHealthReportTask(@Param("date")String date){
        Date nowDate = DateUtils.getNowDate();
        if(date != null){
            nowDate = DateUtils.parseDate(date);
        }
        healthReportStatisticsService.createHealthReportByDay(nowDate);
    }
}
